package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.itheim.client.pojo.Dish;
import com.itheim.client.vo.CategoryVo;
import com.itheima.admin.pojo.Flavors;
import com.itheima.client.mapper.CCategoryMapper;
import com.itheima.client.mapper.CDishMapper;
import com.itheima.client.mapper.CFlavorsMapper;
import com.itheima.service.CDishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author longbosheng
 * @Date 2022/6/19 19:50
 * @PackageName:com.itheima.service.Impl
 * @ClassName: DishServiceImpl
 * @Description: TODO
 * @Version 1.0
 */
@Service
public class CDishServiceImpl extends ServiceImpl<CDishMapper,Dish> implements CDishService {

    @Autowired
    private CDishMapper dishMapper;
    @Autowired
    private CCategoryMapper categoryMapper;
    @Autowired
    private CFlavorsMapper flavorsMapper;

    /**
     * 菜品列表
     * @param categoryId
     * @param status
     * @return
     */
    @Override
    public List<Dish> dishList(String categoryId, int status) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(categoryId != null, Dish::getCategoryId, categoryId);
        /*只查询起售的菜品*/
        wrapper.eq(Dish::getStatus, status);
        /*按照sort排序，如果顺序相同就按照更新时间排序*/
        wrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> list = dishMapper.selectList(wrapper);
        int i = 0;
        /*增加口味表和对应的菜品分类*/
        for (Dish dish1 : list) {
            List<Flavors> flavors = flavorsMapper.selectFlavors(dish1.getId());
            CategoryVo categoryVo = categoryMapper.selectById(dish1.getCategoryId());
            dish1.setCategoryName(categoryVo.getName());
            dish1.setFlavors(flavors);

            System.out.println("添加口味和分类名后的内容：" + dish1);
            i++;
            if (i == list.size()) {
                return list;
            }
        }
        return null;
    }
}
